<style type="text/css">
#production_decompose span.error {
      position: absolute !important;
      left: 148px;
}
#production_decompose tr td div {
  height: 26px;
  padding-top: 3px;
}
#production_decompose tr td div input{
    float: left;
}
</style>
<form method="post" id="productionOrder_issued" action="${rc.getContextPath()}/manage/productionOrder/saveProductionControl" class="pageForm required-validate" onsubmit="return validateCallback(this, dialogAjaxDone)">
<div class="pageContent">
    <input type="hidden" name="navTabId" value="${navTabId!''}" />
    <div class="pageFormContent">
        <fieldset>
            <dl>
                <dt><@label key='productionOrderNum.C,colon'>生产订单编号：</@label></dt>
                <dd><input type="text" name="productionOrderNum" maxlength="20" value="${productionOrder.productionOrderNum!''}" class="required" readonly="readonly"></dd>
            </dl>
            <dl>
                <dt><@label key='plannedQuantity.C,colon'>计划数量：</@label></dt>
                <dd><input type="text" class="digits" name="plannedQuantity" readonly="readonly" min="0" value="${productionOrder.plannedQuantity!''}"/></dd>
            </dl>
            <dl>
                <dt><@label key='releasedQuantity.C,colon'>已下达数量：</@label></dt>
                <dd><input type="text"  class="digits" name="releasedQuantity" readonly="readonly" min="0" value="${productionOrder.releasedQuantity!''}"/></dd>
            </dl>
        </fieldset>
    </div>
    <div class="panelBar">
        <ul class="toolBar">
            <li><a class="add" target="addSFC" title="<@label key='add.B'>新增</@label>"><span><@label key="add.B">添加工作中心</@label></span></a></li>
        </ul>
    </div>
    <table id="production_decompose" class="table canopration" width="100%" layoutH="180">
        <thead>
            <tr>
                <th width="40"><@label key="serialNumber.C">序号</@label></th>
                <th width="100"><@label key='workCenter.C,code.C'>工作中心</@label></th>
                <th width="80"><@label key='plannedQuantity.C'>计划数量</@label></th>
                <th width="50"><@label key="opera.C">操作</@label></th>
            </tr>
        </thead>
        <tbody>
            
        </tbody>
    </table>
    <div class="formBar">
        <ul>
            <li onclick="productionDecomposeJs._issuedProductionOrder();"><div class="buttonActive"><div class="buttonContent"><button type="button">下达</button></div></div></li>
            <li>
                <div class="buttonActiveGrey"><div class="buttonContent"><button type="button" class="close">取消</button></div></div>
            </li>
        </ul>
    </div>
</div>
</form>
<#if workCenters??>
    <div id="workcenters" style="display: none;">
        <select>
            <#list workCenters as workCenter>
                <option value="${workCenter.workCenterCode!''}" <#if workCenter.workCenterCode == defalutWorkCenters>selected</#if>>${workCenter.workCenterText!''}</option>
            </#list>
        </select>
    </div>
</#if>
<script type="text/javascript">
 var productionDecomposeJs = {
        
		count : 0,
		
		unReleaseCount : null,
		
		init : function() {
			var plannedQuantity = parseInt($("input[name='plannedQuantity']", $.pdialog.getCurrent()).val());
			var releasedQuantity = parseInt($("input[name='releasedQuantity']", $.pdialog.getCurrent()).val());
			productionDecomposeJs.unReleaseCount = plannedQuantity - releasedQuantity;
		},
        _addSFC : function(){
            var $tr = $("<tr/>");
            var trId = "tr_"+this.count;
            $tr.attr("id",trId);
            for(var i = 0; i < 4; i++) {
                 $tr.append('<td/>');
            }
            var plannedQuantity = productionDecomposeJs.unReleaseCount;
            $("input[name$='.plannedQuantity']").each(function(i, n){
            	plannedQuantity -= parseInt($(this).val());
            });
            var $tbody = $("#production_decompose .gridTbody", $.pdialog.getCurrent()).find("table tbody");
            $("td:eq(0)", $tr).append(this._getIndex($tbody));
            $("td:eq(1)", $tr).append(this._getWorkCenters("controls["+productionDecomposeJs.count+"].workCenterCode"));
            $("td:eq(2)", $tr).append("<div><input type='text' name='controls["+productionDecomposeJs.count+"].plannedQuantity' value='" + plannedQuantity + "' min='1' class='textInput valid required digits'/></div>");
            $("td:eq(3)", $tr).append("<div><a title='<@label key='delete.B'>删除</@label>' target='ajax' class='btnDel' onclick='productionDecomposeJs._delSFC(\""+trId+"\");'><@label key='delete.B'>删除</@label></a></div>");
            $tr.appendTo($tbody);
            $.jTableTool.initGridColumnWidth($tbody.parents('.grid:first'));
            this.count ++;
        },
        
        _getIndex : function($tbody) {
            var $tr = $($tbody).find("tr");
            if($tr){
                return $tr.length + 1;
            }
            return 1;
        },
        
        _getWorkCenters : function(name){
            var $select = $("div#workcenters", $.pdialog.getCurrent()).html();
            var div = $("<div/>");
            var select = $($select);
            $(select).attr("name", name)
            div.append(select);
            return div;
        },
        
        _delSFC : function(_trId) {
            var evt = window.event || arguments.callee.caller.arguments[0]
            evt.stopPropagation();
            if(_trId){
                var $tbody = $("#production_decompose .gridTbody", $.pdialog.getCurrent()).find("table tbody");
                $tbody.find("tr#"+_trId).remove();
                //重写序号 
                $tbody.find("tr").each(function(i, n){
                    $("td:eq(0)", $(this)).html(i+1);
                });
                $.jTableTool.initGridColumnWidth($tbody.parents('.grid:first'));
            }
        },
        
        _issuedProductionOrder : function() {
            var childCout = 0;
            $("input[name$='].plannedQuantity']", $.pdialog.getCurrent()).each(function(i,n){
                var _value = $(this).val();
                if(!_value || _value == "")
                    _value = 0;
                childCout += parseInt(_value);
            });
            if(childCout > productionDecomposeJs.unReleaseCount){
                alertMsg.error("<@label key='order_releaseQuantity_Out.M'>下达的数量为超过订单未下达的数量，请修改!</@label>");
                return false;
            }else{
                $("#productionOrder_issued", $.pdialog.getCurrent()).submit();
            }    
        }     
 }
 $(function(){
	    $("a[target=addSFC]", $.pdialog.getCurrent()).each(function(){
	        var $this = $(this);
	        $this.click(function(event){
	            productionDecomposeJs._addSFC();
	            event.preventDefault();
	        })
	    }); 
	    productionDecomposeJs.init();
})
</script>